package com.thingcom.delinrobot.main.Launch;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.thingcom.delinrobot.R;
import com.thingcom.delinrobot.base.BaseMainFragment;
import com.thingcom.delinrobot.main.Launch.Login.LoginMailFragment;
import com.thingcom.delinrobot.main.Launch.NewUser.ChooseUserAreaBackFragment;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * author : YuFeng
 * e-mail : 923462992@qq.com
 * date   : 2019/11/27
 */
public class WelcomeMainFragment extends BaseMainFragment {

    @BindView(R.id.btn_welcome_new_id)
    Button btnWelcomeNewId;
    @BindView(R.id.btn_welcome_login)
    Button btnWelcomeLogin;
    @BindView(R.id.iv_welcome_logo)
    ImageView ivWelcomeLogo;
    @BindView(R.id.tv_welcome_one)
    TextView tvWelcomeOne;
    @BindView(R.id.tv_welcome_two)
    TextView tvWelcomeTwo;


    public static WelcomeMainFragment newInstance() {
        Bundle args = new Bundle();
        WelcomeMainFragment fragment = new WelcomeMainFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main_welcome, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }

    @OnClick({R.id.btn_welcome_new_id, R.id.btn_welcome_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_welcome_new_id:
                start(ChooseUserAreaBackFragment.newInstance("Chinese mainland"));
                break;
            case R.id.btn_welcome_login:
                start(LoginMailFragment.newInstance());
                break;
        }
    }

    private void initView(){
        Handler handler = new Handler();
        ObjectAnimator animator1 = ObjectAnimator.ofFloat(ivWelcomeLogo, "alpha", 0, 1);
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(ivWelcomeLogo, "translationY", -300, 0);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(animator1, animator2);
        animatorSet.start();
        PropertyValuesHolder holder1 = PropertyValuesHolder.ofFloat("scaleX", 1, 1);
        PropertyValuesHolder holder2 = PropertyValuesHolder.ofFloat("scaleY", 1, 1);
        PropertyValuesHolder holder3 = PropertyValuesHolder.ofFloat("alpha", 0, 1);

        handler.postDelayed(() -> {
            tvWelcomeOne.setVisibility(View.VISIBLE);
            ObjectAnimator.ofPropertyValuesHolder(tvWelcomeOne, holder1, holder2, holder3).start();
        },500);

        handler.postDelayed(() -> {
            tvWelcomeTwo.setVisibility(View.VISIBLE);
            ObjectAnimator.ofPropertyValuesHolder(tvWelcomeTwo, holder1, holder2, holder3).start();
        },1000);
    }


}
